package com.hdoit.ares.web.advertisement.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.advertisement.model.Advertisement;
import com.hdoit.ares.logical.advertisement.service.IAdvertisementService;
import com.hdoit.ares.logical.viewmodel.advertisement.AdvertisementSearch;
import com.hdoit.ares.selfcommon.enums.advertisement.OpenState;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.file.service.IOssFileInfoService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年03月13日 19:26:18
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/advertisement/advertisement/")
public class AdvertisementController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/advertisement/advertisement/";
    }

    private IAdvertisementService advertisementService;
    public IAdvertisementService getAdvertisementService() {
        return advertisementService;
    }
    @Resource
    public void setAdvertisementService(IAdvertisementService advertisementService) {
        this.advertisementService = advertisementService;
    }

    private IOssFileInfoService ossFileInfoService;
    public IOssFileInfoService getOssFileInfoService() {
		return ossFileInfoService;
	}
    @Resource
	public void setOssFileInfoService(IOssFileInfoService ossFileInfoService) {
		this.ossFileInfoService = ossFileInfoService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="advertisementDetailed";
    private final String saveViewName="save";
    private final String cancelUploadViewName = "cancelUpload";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,AdvertisementSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(advertisementService.queryCount(search));
        List<Advertisement> advertisements= advertisementService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("advertisements", advertisements);
        model.addAttribute("advertisementSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }
   
    @ResponseBody
    @RequestMapping(value = cancelUploadViewName, method = { RequestMethod.GET,RequestMethod.POST },produces = "text/plain;charset=utf-8")
    public String cancelUpload(Model model,String imgUrl){
        return JsonUtils.toJson(ossFileInfoService.deleteModel(imgUrl));
    }
    

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String advertisementDetailed(Model model,String key){
        Advertisement advertisement=new Advertisement();
        advertisement.setIsOpen(OpenState.Open);
        if(!StringUtils.isEmpty(key)){
            advertisement=advertisementService.queryModel(key);
        }
        
        model.addAttribute("advertisement", advertisement);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Advertisement advertisement){
        if(StringUtils.isEmpty(advertisement.getId())){
            advertisement.setId(CommonUtils.getKeyId());
            advertisement.setCreateTime(DateUtils.getCurrent());
            advertisement.setCreateUserId(getLoginModel().getLoginId());
            advertisement.setCreateUserName(getLoginModel().getLoginName());
            advertisement.setModelStatus(ModelStatus.add);
        }else{
            advertisement.setModifyDate(DateUtils.getCurrent());
            advertisement.setModifyUserId(getLoginModel().getLoginId());
            advertisement.setModifyUserName(getLoginModel().getLoginName());
            advertisement.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(advertisementService.applyChange(advertisement)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("advertisement", advertisement);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

   /* @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(advertisementService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }*/
}